#include <cstdio>
#include <cstring>

struct Trie
{
    Trie *next[ 26 ];
    char tr[ 11 ];
    bool ctr;
    Trie( )
    {
        int i;
        for ( i = 0; i < 26; i++ )
            next[ i ] = NULL;
        ctr = false;
    }
};

Trie *head;

void init( )
{
    int i;
    char data1[ 11 ], data2[ 11 ];
    Trie *p;
    head = new Trie;
    scanf("%s", data1);
    while ( scanf("%s", data1) && data1[ 0 ] != 'E' )
    {
        scanf("%s", data2);
        p = head;
        for ( i = 0; i < strlen( data2 ); i++ )
        {
            if ( p->next[ data2[ i ] - 'a' ] == NULL )
                p->next[ data2[ i ] - 'a' ] = new Trie;
            p = p->next[ data2[ i ] - 'a' ];
        }
        strcpy( p->tr, data1);
        p->ctr = true;
    }
}

void work( )
{
    char data[ 3001 ];
    int i, j, t;
    Trie *p;
    gets( data );
    gets( data );
    while ( gets( data ) && data[ 0 ] != 'E' )
    {
        p = head;
        t = -1;
        for ( i = 0; i < strlen( data ); i++ )
        {
            if ( data[ i ] < 'a' || data[ i ] > 'z' )
            {
                if ( p != head && p->ctr )
                    printf("%s", p->tr);
                else
                    for ( j = t + 1; j < i; j++ )
                        printf("%c", data[ j ]);
                p = head;
                t = i;
                printf("%c", data[ i ]);
            }
            else
            {
                p = p->next[ data[ i ] - 'a' ];
                if ( !p )
                {
                    for ( j = t + 1; ; j++ )
                    {
                        if ( data[ j ] < 'a' || data[ j ] > 'z' )
                            break;
                        printf("%c", data[ j ]);
                    }
                    t = i = j - 1;
                    p = head;
                }
            }
        }
        printf("\n");
    }
}

int main( )
{
    init( );
    work( );
    return 0;
}
